#include "modbustcpdialog.h"
#include "ui_modbustcpdialog.h"
#include <QThread>
#include <QProgressBar>

ModbusTcpDialog::ModbusTcpDialog(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusTcpDialog)
{
    ui->setupUi(this);
    ui->ModProgress->setVisible(false);
}

ModbusTcpDialog::~ModbusTcpDialog()
{
    delete ui;


}

void ModbusTcpDialog::initAllPath(){
    if(!close_type){
    t = new QThread;
    mythread = new MyThread;

    mythread->moveToThread(t);

    ui->ModProgress->setVisible(true);

   if(TYPE == false){
        qDebug()<<"--------------------------------------------------------------------"<<mythread->thread();
        qDebug()<<"0.创建ModBusTcp线程 单点测量。"<<"thread:"<<mythread->thread();
        connect(this,&ModbusTcpDialog::Connect_to_modbus_Simple,mythread,&MyThread::recviceIP);
        emit Connect_to_modbus_Simple(IP_address,TYPE,TOKEN,SaveFloder);
    }else{
        qDebug()<<"0.创建ModBusTcp线程 多点测量。";
        connect(this,&ModbusTcpDialog::Connect_to_modbus_All,mythread,&MyThread::recviceIP);
        emit Connect_to_modbus_All(IP_address,TYPE,TOKEN,SaveFloder);
    }


   connect(mythread,&MyThread::ChartMOd,this,&ModbusTcpDialog::reciveMod);
   connect(mythread,&MyThread::ChartMOd,t,[this](){
//       QMessageBox::information(this,"成功","采集数据成功！");
       ui->ModProgress->setValue(100);
       finishThread();
       initAllPath();
      });

   connect(mythread,&MyThread::errorClient,t,[this](){
       QMessageBox::information(this,"错误","连接未成功！");
       finishThread();

   });
   connect(mythread,&MyThread::errorData,t,[this](){
       QMessageBox::information(this,"错误","数据未采全！");
       finishThread();
   });
   connect(mythread,&MyThread::errorType,t,[this](){
       QMessageBox::information(this,"错误","模式选择错误！");
       finishThread();
   });


   connect(mythread,&MyThread::errorUpload,this,&ModbusTcpDialog::UploadErrorInfo);
   connect(mythread,&MyThread::errorUpload,t,[this](){
       finishThread();
   });
   t->start();
}

}
void ModbusTcpDialog::finishThread()
{
    t->quit();
    t->wait();

    mythread->deleteLater();
    t->deleteLater();
    ui->ModProgress->setValue(0);
    ui->ModProgress->setVisible(false);
}
void ModbusTcpDialog::UploadErrorInfo(QString errorInfo)
{
     QMessageBox::information(this,"错误",errorInfo);
}
int i = 0;
void ModbusTcpDialog::reciveMod(QVector<float> data,QString fileName)
{

     DATA = data;
     qDebug()<<"6.采集的数据:"<<DATA<<"thread:"<<mythread->thread();
     qDebug()<<"7.ModBusTcp通信数据采集完成！"<<"thread:"<<mythread->thread();
     qDebug()<<"--------------------------------------------------------------------"<<mythread->thread();
     QStringList itemsList;
//     for(int i = 0 ; i<DATA.size();i++){

          QString Desi = "第"+QString::number(i+1,10)+"采集成功:"+fileName;
          i++;
         itemsList<<Desi;
//     }
        ui->listWidget->addItems(itemsList);

}

void ModbusTcpDialog::on_AllPushButton_clicked()
{
    IP_address = ui->InputEdit_ip->text();
    close_type = false ;
    TYPE = true;
    if(!IP_address.isEmpty()){
        ui->listWidget->clear();
        i = 0;
        initAllPath();
    }else{
        QMessageBox::information(this,"错误","请填写IP地址");
    }
}





void ModbusTcpDialog::on_SimplePushButton_clicked()
{
    IP_address = ui->InputEdit_ip->text();
    close_type = false ;
    TYPE = false;
    if(!IP_address.isEmpty()){
            ui->listWidget->clear();
            i = 0;
            initAllPath();
    }else{
         QMessageBox::information(this,"错误","请填写IP地址");
    }





}


void ModbusTcpDialog::saveTokenAndFloder(QString token,QString floder){
    this->TOKEN = "Bearer " +token;
    qDebug()<<"获取到的token:"<<TOKEN;
     this->SaveFloder = floder;
    qDebug()<<"获取到的保存路径:"<<SaveFloder;

}

void ModbusTcpDialog::on_stopButton_clicked()
{
    close_type = true;
    qDebug()<<"终止采集";
    finishThread();
    QMessageBox::information(this,"操作","终止采集成功");
}
